package cn.tedu.collections;

import java.util.*;

/**
 * Date：2021年11月
 * Author：程Sir
 * Name： JavaSE-API
 * Content:
 *
 *      Comparable 被称之为内部比较器，是因为该比较器与当前自定义类有强耦合关系，比较规则写在当期期定义类的里面
 *
 */
public class Student implements Comparable<Student> {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    /*
        compareTo方法的返回值时int类型，比较的结果只看正负和是否是0，跟值的大小无关。
        如果当前对象-比较对象之后的值 > 0 ,正序
        如果当前对象-比较对象之后的值 = 0 ,相同
        如果当前对象-比较对象之后的值 < 0 ,倒序
     */
    @Override
    public int compareTo(Student o) {
        return  this.age - o.age;
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Collections.addAll(list,
                new Student("刘备",66),
                new Student("关羽",66),
                new Student("张飞",77),
                new Student("曹操",33));
        // 排序
        Collections.sort(list);
        System.out.println(list);

    }


}
